package com.yidi.system.repository;

import com.yidi.common.core.Repository.BaseRepository;
import com.yidi.common.core.domain.entity.SysRole;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author ：Xn
 * @ClassName: SysRoleRepository
 * @date ：2021-10-19 14:43
 * @describe ：角色
 */
@Repository
public interface SysRoleRepository extends BaseRepository<SysRole,Long> {

    SysRole findByRoleName(String roleName);

    SysRole findByRoleKey(String roleKey);

    @Query(nativeQuery = true, value = "select r.id from sys_role r left join sys_user_role ur on ur.role_id = r.id" +
            " left join sys_user u on u.id = ur.user_id where u.id = ?1")
    List<Long> selectRoleListByUserId(Long userId);


    @Query(nativeQuery = true, value = "select distinct r.id, r.role_name as roleName, r.role_key as roleKey, r.order_num as orderNum, r.data_scope as dataScope, r.menu_check_strictly as menuCheckStrictly, r.dept_check_strictly as deptCheckStrictly,r.status, r.remove_status as removeStatus, r.create_time as createTime, r.remark from sys_role r" +
            " left join sys_user_role ur on ur.role_id = r.id" +
            " left join sys_user u on u.id = ur.user_id" +
            " left join sys_dept d on u.dept_id = d.id" +
            " where r.remove_status = 0 and ur.user_id = ?1")
    List<Map<String, Object>> selectRolePermissionByUserId(Long userId);
}
